SQL SERVER – 2005 – Find Tables With Foreign Key Constraint in Database

A foreign key inventory needs both the constraint and its column pairings. I include schema names to identify each table clearly.

A gouache terraced garden links smaller planting modules to a larger supporting terrace through visible fittings.

After writing about tables without a clustered index or primary key, I wanted the corresponding foreign-key inventory. The original script already used the right two catalog views, despite its introductory label mistakenly saying primary key.

SELECT fk.name AS foreign_key,
       OBJECT_SCHEMA_NAME(fk.parent_object_id) AS child_schema,
       OBJECT_NAME(fk.parent_object_id) AS child_table,
       COL_NAME(fkc.parent_object_id, fkc.parent_column_id) AS child_column,
       OBJECT_SCHEMA_NAME(fk.referenced_object_id) AS parent_schema,
       OBJECT_NAME(fk.referenced_object_id) AS parent_table,
       COL_NAME(fkc.referenced_object_id, fkc.referenced_column_id) AS parent_column,
       fkc.constraint_column_id, fk.is_disabled, fk.is_not_trusted
FROM sys.foreign_keys AS fk
JOIN sys.foreign_key_columns AS fkc ON fkc.constraint_object_id = fk.object_id
ORDER BY child_schema, child_table, foreign_key, fkc.constraint_column_id;

Run this in the database you intend to inspect. Schema names distinguish tables with the same name. constraint_column_id preserves the pairing order in a composite foreign key.

Check enforcement and trust

is_disabled and is_not_trusted help distinguish a declared relationship from a trusted, enabled constraint. Metadata visibility depends on permissions; an empty result is not proof that no relationships exist anywhere.

Foreign keys enforce declared relationships. I choose them from the data model. They don’t automatically create indexes on referencing columns. Those access paths need separate investigation.

Related reading

A foreign key is not an automatic access path, it is an enforced relationship when enabled.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Constraint and Keys, SQL Scripts, SQL Server, SQL System Table
Previous Post
SQL SERVER – 2005 – Search Stored Procedure Code – Search Stored Procedure Text
Next Post
SQL SERVER – 2005 – Find Tables With Primary Key Constraint in Database

Related Posts

48 Comments. Leave new

  • bharath kurra
    July 18, 2013 8:38 am

    hi
    i had a problem..
    suppose a primary table had a foreign key with Secondary table.how can we know the data present in primary table is present secondary table or not?

    Reply
    • You can do this

      select case when s.col is null ‘not present’ else ‘present’ end as status from primary_table as p
      left join secondary_table as s on p.key_col=s.key_col

      Reply
  • TELL HOW TO ACESS MASTER TABLE USING FOREIGN KEY

    Reply
  • the code is not providing me any output , it shows no output …does that mean there are no foreign keys in the database .

    Reply
  • it saves my time alot
    Thank you so much

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.